package com.yfapp.platform.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.yfapp.platform.dao.CustDao;
import com.yfapp.platform.entity.Admin;
import com.yfapp.platform.entity.Cust;
import com.yfapp.platform.entitytest.TCust;
import com.yfapp.platform.entitytest.TCustId;
import com.yfapp.platform.tools.Page;
import com.yfapp.platform.tools.Pagination;

@Repository
public class CustDaoImpl extends BaseDaoImpl<Admin, Integer> implements CustDao {

	private static final TCustId TCustId = new TCustId();

	public List<TCustId> getCustList() {
		String sql = "select * from t_Cust where FCustID < 500";
		List<TCustId> listCust = dbUtilsTemplate.queryForList(TCustId.class, sql);
		
		String sql2 = "select * from (select *,ROW_NUMBER() OVER (order by FCustID) AS ROWNUM from t_Cust) t where ROWNUM between 89981 and 90000";
		List<TCustId> listCust2 = dbUtilsTemplate.queryForList(TCustId.class, sql2);
		
		int gettbCount = dbUtilsTemplate.gettbCount("t_Cust", TCustId);
		System.out.println(gettbCount);
		Page page = new Page();
		page.setPage(1);
		page.setRows(20);
		List<com.yfapp.platform.entitytest.TCustId> beanLit = dbUtilsTemplate.getBeanLit(1, "FCustID", "t_Cust", TCustId, page);
		//System.out.println("beanlit="+beanLit.toString());
		return beanLit;
	}
	/*
	//分页
	@SuppressWarnings("unchecked")
	public <T> List<T> getBeanLit(int count2,String orderBy,String tb,T ob,Page page){
		
		int count = this.gettbCount(tb, ob);
		int rows = page.getRows();//本页数
		int total = (int) Math.ceil((double)count / (double) rows);//总页数
		int page2 = page.getPage();//第几页
		int page3 = page2 > total ? total : page2;//第几页
		int PageNum = (page3 == total ? (count-(total-1)*page.getRows()) : page.getRows());//本页数
		
		//String sql = "select * from (select *,ROW_NUMBER() OVER (order by FCustID) AS ROWNUM from t_Cust) t where ROWNUM between 21 and 40";
		String sql = "select * from (select *,ROW_NUMBER() OVER (order by "+orderBy+") AS ROWNUM from "+tb+") t where ROWNUM between "+(page3-1)*rows+" and "+((page3-1)*rows+PageNum);
		List<T> listCust2 = (List<T>) dbUtilsTemplate.queryForList(ob.getClass(), sql);
		System.out.println("size="+listCust2.size());
		
		return listCust2;
	}
	
	public <T> Pagination<T> list(List<T> list2,Page page,String tb,T ob){
		
		int count = this.gettbCount(tb, ob);
		int rows = page.getRows();//本页数
		int total = (int) Math.ceil((double)count / (double) rows);//总页数
		int page2 = page.getPage();//第几页
		int page3 = page2 > total ? total : page2;//第几页
		int PageNum = (page3 == total ? (count-(total-1)*page.getRows()) : page.getRows());//本页数
		
		Pagination<T> pagination = new Pagination<T>((List<T>) list2,page3,count,PageNum,total);
		
		return pagination;
	}
	
	public <T> int gettbCount(String tb,T ob){
		String sql="select count(*) from "+tb;
		Object count = dbUtilsTemplate.getCount(sql,null);
		int num = Integer.valueOf(count.toString());
		return num;
	}*/

	@SuppressWarnings("unchecked")
	public <T> Pagination<T> getCustQuery(Page page) {
		int gettbCount = dbUtilsTemplate.gettbCount("t_Cust", TCustId);
		List<com.yfapp.platform.entitytest.TCustId> beanLit = dbUtilsTemplate.getBeanLit(gettbCount, "FCustID", "t_Cust", TCustId, page);
		Pagination<T> list = (Pagination<T>) dbUtilsTemplate.list(beanLit, page, "t_Cust", TCustId);
		return list;
	}

	public com.yfapp.platform.entitytest.TCustId getCustByIDcard(String idcard) {
		String sql = "select * from t_Cust where FCustID = '"+idcard+"' ";
		com.yfapp.platform.entitytest.TCustId cust = dbUtilsTemplate.query(TCustId.class, sql);
		return cust;
	}
}
